package main

import (
	"fmt"
	"os"
	"strconv"
	"strings"
)

func task01(n int) int {

	if n <= 0 {
		fmt.Printf("n: %v <= 0", n)
		os.Exit(2)
	}
	if n == 1 {
		return 1
	}
	return n * task01(n-1)
}
func task1f(n int) int {
	if n <= 1 {
		fmt.Printf("n:%v <= 0", n)
		os.Exit(2)
	}
	rest := 1
	for i := n; i >= 1; i-- {
		rest *= i
	}
	return rest
}

func task2(n int) {
	//获取最后一行长度
	_, l := getLen(n)
	rest(n, l)
}
func rest(n, len int) {
	for i := 1; i <= n; i++ {
		//获取当前行长度,和当前行的真实值
		s, l := getLen(i)
		//获取最后一行长度和当前长度差值
		c := len - l
		var st strings.Builder
		//根据差值补全空格。
		for j := 0; j <= c; j++ {
			st.WriteString(" ")
		}
		//输出空格
		fmt.Printf("%v", st.String())
		//输出真实值
		fmt.Printf("%v\n", s)
	}
}
func getLen(n int) (string, int) {
	var strList []string
	for j := n; j >= 1; j-- {
		strList = append(strList, strconv.Itoa(j))
	}
	nStr := strings.Join(strList, " ")
	return nStr, len(nStr)
}
func main() {
	fmt.Println("第一题~~~~~~~~~~~~~~~~~~~~~")
	n := 7
	fmt.Printf("%v的阶乘(递归实现)为：%v\n", n, task01(n))
	fmt.Printf("%v的阶乘(for循环实现)为：%v\n", n, task1f(n))
	fmt.Println("第二题~~~~~~~~~~~~~~~~~~~")
	task2(20)

}
